<?php require_once('../Connections/tecno.php'); ?>
<?php
define('FPDF_FONTPATH','font/');
require('../fpdf16/mysql_table.php');
session_start();
//$id = "1";
if (isset($_REQUEST['id'])) {
	$id = $_REQUEST['id'];
}
//////////////////////////////////////////////////// 
//Convierte fecha de mysql a normal 
//////////////////////////////////////////////////// 
function cambiaf_a_normal($fecha){ 
    ereg( "([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})", $fecha, $mifecha); 
   $lafecha=$mifecha[3]."-".$mifecha[2]."-".$mifecha[1]; 
    return $lafecha; 
} 
class PDF extends PDF_MySQL_Table
{
function Header()
{
	//Title
	$this->Image('../images/logo.jpg',5,5, 50);
	/* $this->SetFont('Arial','B',16);
	$this->Cell(60,3,'',0,0,'C');
	$this->Cell(50,12,'Ficha del Cliente',0,0,'C'); */
	
	$this->SetFont('Arial','B',7);
	$this->Cell(60,3,'',0,0,'C');
	$this->Cell(23,3,utf8_decode('Dirección Fiscal: '),0,0,'L');	
	$this->SetFont('Arial','',7);
	$this->Cell(80,3,utf8_decode('Av. Bolívar C.C. Amelia Piso 3 Ofic. 306'),0,0,'L');
	$this->SetFont('Arial','I',8);
	$this->Cell(0,3,'Impreso el: '. date('d/m/Y'),0,1,'R');
//Linea 4
	$this->SetFont('Arial','',7);
	$this->SetTextColor(00,00,00);
	$this->Cell(53,3,'',0,0,'C');
	$this->Cell(80,3,'Cagua Edo. Aragua. Telfs: 0244-3964557 / 0426-5125749',0,1,'C');

//Linea 5
	$this->Cell(47,3,'',0,0,'C');
	$this->Cell(80,3,'0416-0458957 / 0426-5180385 / 0424-3261656',0,1,'C');

	
	$this->Ln(6);
	//Ensure table header is output
	parent::Header();
}
}

//Connect to database
mysql_select_db($database_tecno, $tecno);
$query_cliente = "SELECT * FROM datospersonales WHERE id = '$id'";
$cliente = mysql_query($query_cliente, $tecno) or die(mysql_error());
$row_cliente = mysql_fetch_assoc($cliente);
$totalRows_cliente = mysql_num_rows($cliente);

$pdf=new PDF('P','mm','Letter');
$pdf->Open();
$pdf->AddPage();
//Subt�tulo Datos del Personales
$pdf->Cell(3,4,'',0,1);
$pdf->SetFont('times','B',12); 
$pdf->Cell(1,10,'Datos Personales:',0,1,'L');
$pdf->SetFont('Arial','',10);
///Linea 1: se imprime la: C�dula, Nombres y Apellidos
$pdf->SetFont('Arial','B',10);
$pdf->Cell(13,6,utf8_decode('Cédula:'),0,0);
$pdf->SetFont('Arial','',9);
$pdf->Cell(18,6,$row_cliente['cedula'],0,0);
$pdf->SetFont('Arial','B',10);
$pdf->Cell(37,6,'Nombres y Apellidos:',0,0);
$pdf->SetFont('Arial','',9);
$pdf->Cell(55,6,$row_cliente['nombres'].' '.$row_cliente['primerApellido'].' '.$row_cliente['segundoApellido'],0,0);
$pdf->SetFont('Arial','B',10);
//Linea de tel�fonos e email
$pdf->Cell(19,6,utf8_decode('Teléfonos:'),0,0);
$pdf->SetFont('Arial','',9);
$pdf->Cell(53,6,$row_cliente['telefono'].' / '.$row_cliente['celular'],0,0);
//L�nea de espacio
$pdf->Cell(3,4,'',0,1);

mysql_select_db($database_tecno, $tecno);
$query_clienteArt = "SELECT *, Sum(monto) as totalArt FROM detallescliente WHERE id = '$id' AND status <> '3' GROUP BY idArt ORDER BY giro DESC";
$clienteArt = mysql_query($query_clienteArt, $tecno) or die(mysql_error());
$row_clienteArt = mysql_fetch_assoc($clienteArt);
$totalRows_clienteArt = mysql_num_rows($clienteArt);

if ($totalRows_clienteArt >0){
do {//table: 

$idArt=$row_clienteArt ['idArt'];

//$totalArticulo = "SELECT Sum(cantidad) as pagado FROM pagos WHERE id = '$id' AND idArt = '$idArt' AND status = 0";
//$tot = mysql_query($totalArticulo, $tecno) or die(mysql_error());
//$row_tot = mysql_fetch_assoc($tot);
//$totalRows_tot = mysql_num_rows($tot);
$totalArticulo = "SELECT Sum(monto) as pagado FROM detallescliente WHERE id = '$id' AND idArt = '$idArt' AND status = 1";
$tot = mysql_query($totalArticulo, $tecno) or die(mysql_error());
$row_tot = mysql_fetch_assoc($tot);
$totalRows_tot = mysql_num_rows($tot);

$pronto = "SELECT Sum(monto) as prontoPago FROM detallescliente WHERE id = '$id' AND idArt = '$idArt' AND status = 3";
$pro = mysql_query($pronto, $tecno) or die(mysql_error());
$row_pro = mysql_fetch_assoc($pro);
$totalRows_pro = mysql_num_rows($pro);


$total = $row_clienteArt['totalArt'];
$pagado = $row_tot['pagado'];
$prontoPago = $row_pro['prontoPago'];

$total = round($total,2);
$pagado = round($pagado,2);
    $debe = ($total - $pagado);
//$debe = round($debe,2);


$pdf->Ln(6);

//Detalle
$query = "SELECT * FROM detallescliente, statuspagos WHERE detallescliente.idArt = '$idArt' AND statuspagos.idStatus = detallescliente.status ORDER BY giro ASC";
$query1 = mysql_query($query, $tecno) or die(mysql_error());
$row_query = mysql_fetch_assoc($query1);
$totalRows_query = mysql_num_rows($query1);
if ($totalRows_query > 0) {

//Titulos Tabla
$pdf->SetFont('arial','B',9); 
$pdf->SetDrawColor(0);
$pdf->SetFillColor(235,235,235);
$pdf->SetTextColor(0);
$pdf->SetLineWidth(0.15);
//$pdf->Cell(20,6,'',0,0,'C');
$pdf->Cell(58,6,'Producto',1,0,'C',true);
$pdf->Cell(18,6,'Cuota',1,0,'C',true);
$pdf->Cell(24,6,'Fecha',1,0,'C',true);
$pdf->Cell(25,6,'Estatus',1,0,'C',true);
$pdf->Cell(30,6,'Fecha de Pago',1,0,'C',true);
$pdf->Cell(30,6,'Monto Pagado',1,1,'C',true);

$pdf->Ln(3);

$color=array("235,235,235","255,255,255");
$conta_color=0;

do {
$monto=0;	
$tipo=$row_query['status'];
switch ($tipo) {
	case 0:
		$tipo="Sin Pagar";
		$monto=0;
	break;
	case 1:
		$tipo="Pagada";
		$monto=$row_query['monto'];
	break;
	case 2:
		$tipo="Abonada";
		$monto=$row_query['abono'];
	break;	
	case 3:
		$tipo="Pronto Pago";
		$monto=0;
	break;	
}
$conta_color++;
$fill= $color[$conta_color%2];
$pdf->SetFillColor($fill);
//$pdf->Cell(20,5,'',0,0,'C',false);
$pdf->Cell(58,5,substr($row_query['productoDes'],0,40),0,0,'L',true);
$pdf->Cell(18,5,$row_query['giro'],0,0,'C',true);
$pdf->Cell(24,5,cambiaf_a_normal($row_query['fechaV']),0,0,'C',true);
$pdf->Cell(25,5,$tipo,0,0,'C',true);
$pdf->Cell(30,5,cambiaf_a_normal($row_query['fechaP']),0,0,'C',true);
$pdf->Cell(30,5,number_format($monto, 2, ',', '.'),0,1,'R',true);

//$pdf->Cell(30,5,$monto,0,1,'C',true);
//$pdf->Cell(30,5,$row_query['status'],0,0,'C',true);
//$pdf->Cell(30,5,cambiaf_a_normal($row_query['fecha']),0,0,'C',true);
//$pdf->Cell(30,5,number_format($row_query['monto'], 2, ',', '.'),0,0,'C',true);


/*
$pdf->Cell(30,5,$status,0,1,'C',true);
	*/
} while ($row_query = mysql_fetch_assoc($query1));
}


$pdf->Ln(6);
$pdf->SetFont('Arial','B',10);
$pdf->Cell(40,6,' ',0,0);
$pdf->Cell(25,6,'Total Articulo:',0,0);
$pdf->SetFont('Arial','',9);
$pdf->Cell(19,6,$total,0,0);

$pdf->SetFont('Arial','B',10);
$pdf->Cell(25,6,'Total Pagado:',0,0);
$pdf->SetFont('Arial','',9);
$pdf->Cell(19,6,$pagado,0,0);

$pdf->SetFont('Arial','B',10);
$pdf->Cell(25,6,'Saldo Deudor:',0,0);
$pdf->SetFont('Arial','',9);
$pdf->Cell(19,6,$debe,0,0);
$pdf->Ln(10);

} while ($row_clienteArt = mysql_fetch_assoc($clienteArt));
}
$pdf->Output();
?>